package intelligentcity.model;

import intelligentcity.model.intersection.Intersection;
import intelligentcity.model.streets.HorizontalStreet;
import intelligentcity.model.streets.Street;
import intelligentcity.model.streets.VerticalStreet;

import java.util.ArrayList;
import java.util.List;

public class CityBuilder {

    private List<Street> streets;
    private List<Intersection> intersections;
    private List<Point> startPoints;

    protected int leftBorderOfTraffic;
    protected int rightBorderOfTraffic;
    protected int upperBorderOfTraffic;
    protected int lowerBorderOfTraffic;
    
    protected CityBuilder() {
        streets = new ArrayList<Street>();
        intersections = new ArrayList<Intersection>();
        startPoints = new ArrayList<>();
        
    }

    public void addStreet(Street street) {
        streets.add(street);
    }

    public void addIntersection(VerticalStreet verticalStreet, HorizontalStreet horizontalStreet) {
        intersections.add(new Intersection(verticalStreet, horizontalStreet));
    }

    public CityBuilder createStartPoints(Point upperLeftCorner, Point bottomRightCorner) {

        for (Street street : streets) {
            // TODO Dziwy
            List<Point> potencialStartPoints = new ArrayList<>(2);
            Point endPoint;
            if (street instanceof HorizontalStreet) {
                endPoint = new Point(street.getFirstStreet().getP2());
                endPoint.incX();
                potencialStartPoints.add(endPoint);
                endPoint = new Point(street.getSecondStreet().getP1());
                endPoint.decX();
                potencialStartPoints.add(endPoint);
            } else {
                endPoint = new Point(street.getFirstStreet().getP1());
                endPoint.incY();
                potencialStartPoints.add(endPoint);
                endPoint = new Point(street.getSecondStreet().getP2());
                endPoint.decY();
                potencialStartPoints.add(endPoint);
            }

            for (Point p : potencialStartPoints) {
                if (!p.inside(upperLeftCorner, bottomRightCorner)) {
                    startPoints.add(p);
                }
            }
        }
        System.out.println();
        return this;
    }

    public CityBuilder createNeighbourhood() {
        for (Street street : streets) {

            List<Point> streetPoints = new ArrayList<>();
            
            streetPoints.add(street.getFirstStreet().getP1());
            streetPoints.add(street.getFirstStreet().getP2());


            for (Intersection intersection : intersections) {
                List<Point> interPoints = new ArrayList<>();
                
                interPoints.add(intersection.getVerticalStreet().getFirstStreet().getP1());
                interPoints.add(intersection.getVerticalStreet().getFirstStreet().getP2());
                interPoints.add(intersection.getHorizontalStreet().getFirstStreet().getP1());
                interPoints.add(intersection.getHorizontalStreet().getFirstStreet().getP2());

                for (Point streetPoint : streetPoints) {
                    for (Point interPoint : interPoints) {
                        if (streetPoint.equals(interPoint)) {
                            street.addNeighbour(intersection);
                            intersection.addNeighbour(street);

                            System.out.println(intersection);
                            System.out.println(intersection.getNeighbors());
                        }

                    }
                }
            }
        }

        return this;
    }

    public City build() {
        return new City(streets, intersections, startPoints, leftBorderOfTraffic, rightBorderOfTraffic,
                upperBorderOfTraffic, lowerBorderOfTraffic);
    }

}
